package com.qingcloud.fexecutor.service;

import com.qingcloud.fexecutor.base.handler.ApiExecuteParam;
import com.qingcloud.fexecutor.base.handler.ApiRequestParam;
import com.qingcloud.fexecutor.mapper.ApiBaseMapper;
import com.qingcloud.fexecutor.mapper.WebPubAppMapper;
import com.qingcloud.fexecutor.mapper.WebPubPageResourceMapper;
import com.qingcloud.fexecutor.model.ApiBase;
import com.qingcloud.fexecutor.model.WebPubApp;
import com.qingcloud.fexecutor.model.WebPubPageResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * 前端应用页面资源管理
 */
@Service
public class WebPageResourceService {

    @Resource
    private WebPubAppMapper webPubAppMapper;

    @Resource
    private WebPubPageResourceMapper webPubPageResourceMapper;

    @Resource
    private ApiBaseMapper apiBaseMapper;

    /**
     * 查询API执行信息
     */
    public ApiRequestParam apiRequestParam(ApiExecuteParam param) {
        //查询资源信息
        Integer appId;
        String realCode;
        WebPubPageResource pageResource = webPubPageResourceMapper.selectOne(new WebPubPageResource().setPageId(param.getPageId()).setResourceCode(param.getResourceCode()));
        WebPubApp webPubApp = webPubAppMapper.selectByPrimaryKey(pageResource.getAppId());
        appId = webPubApp.getDevAppId();
        realCode = pageResource.getRealCode();
        //查询API信息
        ApiBase apiBase = apiBaseMapper.findByCode(appId, Collections.singletonList(realCode)).get(0);
        ApiRequestParam result = new ApiRequestParam();
        result.setApiId(apiBase.getId());
        result.setName(apiBase.getName());
        result.setType(apiBase.getType());
        result.setMethod(apiBase.getMethod());
        result.setUrl(apiBase.getUrl());
        result.setQueryParam(param.getQueryParam());
        result.setHeadParam(param.getHeadParam());
        result.setApiBodyJson(param.getApiBodyJson());
        result.setPathParam(param.getPathParam());
        result.setFormParam(param.getFormParam());
        return result;
    }

}
